package com.gucheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gucheng.common.ResponseResult;
import com.gucheng.mapper.TagMapper;
import com.gucheng.model.dto.TagListDto;
import com.gucheng.model.entity.LoginUser;
import com.gucheng.model.entity.Tag;
import com.gucheng.model.vo.PageVo;
import com.gucheng.model.vo.TagVo;
import com.gucheng.service.TagService;
import com.gucheng.utils.BeanCopyUtils;
import com.gucheng.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2024-09-05 16:29:36
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //分页查询
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(tagListDto.getName()), Tag::getName, tagListDto.getName());
        queryWrapper.eq(StringUtils.hasText(tagListDto.getRemark()), Tag::getRemark, tagListDto.getRemark());

        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, queryWrapper);

        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());

        //封装数据返回
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addTag(TagListDto tagListDto) {
        Tag tag = new Tag();
        tag.setName(tagListDto.getName());
        tag.setRemark(tagListDto.getRemark());
        //设置创建时间
        Date date = new Date();
        tag.setCreateTime(date);
        tag.setUpdateTime(date);
        tag.setDelFlag(0);
        //获取当前用户id
        Long userId = SecurityUtils.getUserId();
        tag.setCreateBy(userId);

        save(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTag(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTagInfo(Long id) {
        Tag tag = getById(id);
        TagVo tagVo = new TagVo();
        tagVo.setId(tag.getId());
        tagVo.setName(tag.getName());
        tagVo.setRemark(tag.getRemark());
        TagVo tagData = BeanCopyUtils.copyBean(tag, TagVo.class);
        return ResponseResult.okResult(tagData);
    }

    @Autowired
    private TagMapper tagMapper;

    @Override
    public ResponseResult updateTag(TagVo tagVo) {
        Tag tag = new Tag();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tag.setUpdateBy(loginUser.getUser().getId());
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = new Date();
            String strNow = sdf.format(now);
            Date date = sdf.parse(strNow);
        }catch (ParseException e){
            e.printStackTrace();
        }
        tag.setId(tagVo.getId());
        tag.setName(tagVo.getName());
        tag.setRemark(tagVo.getRemark());

        tagMapper.updateById(tag);
        return ResponseResult.okResult();
    }

    @Override
    public List<TagVo> listAllTag() {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId,Tag::getName);
        List<Tag> list = list(wrapper);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);
        return tagVos;
    }
}
